//
//  YREmotionViewCell.swift
//  新浪微博
//
//  Created by Viper on 16/7/5.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

class YREmotionViewCell: UICollectionViewCell {
    
    var emotions : [YREmotion]?{
        
        didSet{
            
            //当每次外界给其赋值的时候要把当前cell中的按钮全部隐藏 由于cell复用
            
            for button in emotionsButtons {
                
                button.hidden = true
                
            }
            
            //遍历外界传入的一维数组
            for (i,emotion) in emotions!.enumerate(){
                //获取button
                let button  = emotionsButtons[i]
                
                //显示button
                button.hidden = false
                
                //给其button设置emotion设置数据
                button.emotion = emotion
                
                //判断如果type=0 的时候图片  如果 =  1 代表字符串
                
                if emotion.isEmoji {
                    
                    let title = ((emotion.code ?? "") as NSString).emoji()
                    
                    button.setTitle(title, forState: UIControlState.Normal)
                    
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                    
                }else{
                    
                    let path = emotion.path ?? ""
                    
                    let png = emotion.png ?? ""
                    
                    let name = path + png
                   
                   let image = UIImage(named: name, inBundle:  YREmotionTools.sharedTools.emotionBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)

                    
                }
                
            }
        }
        
    }
    //
    //保存20个表情按钮
    var emotionsButtons : [YREmotionButton] = [YREmotionButton]()
    
    var indextPath : NSIndexPath?{
        
        didSet{
      
//            messageLabel.text = "第\(indextPath?.section ?? 0)组,第\( indextPath?.item ?? 0)页"
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置视图
    
    private func setupUI(){
        addChildButtons()
        addSubview(deleteButton)
//        addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) in
//            
//            make.center.equalTo(contentView)
//            
//        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 记得按钮的宽度高度
        let w = (SCREENW - 10 ) / CGFloat(emotionsMaxCol)
        let h = (216 - 37 - 20) / CGFloat(emotionsMaxRow)
        
        // 遍历emoticonButtons
        for (i , button) in emotionsButtons.enumerate() {
            let x = i % emotionsMaxCol
            let y = i / emotionsMaxCol
            // 设置buttonframe
            button.frame = CGRect(x: CGFloat(x) * w + 5 , y: CGFloat(y) * h, width: w, height: h)
            
            //删除按钮的frame
            deleteButton.frame = CGRect(x: SCREENW -  w - 5, y: h * 2, width: w, height: h)
            
        }
    }
       // 创建20个按钮
    func addChildButtons(){
        for _ in 0..<emitionsMaxCount {
            // 创建button
            let button = YREmotionButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            // 设置随机颜色
            button.backgroundColor = self.backgroundColor
            //按钮点击事件
            button.addTarget(self, action: #selector(YREmotionViewCell.emotionBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            //设置大小
            button.titleLabel?.font = UIFont.systemFontOfSize(33)
            // 添加到数组中
            emotionsButtons.append(button)
            // 添加button
            contentView.addSubview(button)
        }
    }
    func emotionBtnClick(button : YREmotionButton ){
        
//        print("表情按钮")
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionButtonNoti, object: button.emotion)
        
    }
    
    func deleteButtonClick(){
        
//        print("点击删除按钮")
    
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionDeledeNoti, object: nil)
        
    }
    
    //MARK: - 懒加载
    private lazy var messageLabel : UILabel = {
       
        let label = UILabel(textColor: UIColor.redColor(), textFont: 25)
        label.textAlignment = .Center
        
        return label
    }()
    
    private lazy var deleteButton : UIButton = {
       
        let button = UIButton()
        button.setImage(UIImage(named:"compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"compose_emotion_table_left_normal"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: #selector(YREmotionViewCell.deleteButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
